<?php

namespace model\admin;
use model;
class goodsModel extends model{

    public static function get_list( $filter = [] ){

        $where = '';
        if( !empty($filter['name']) ){
            $where .= "`name` like '%" . $filter['name'] . "%'";
        }



        $sql = 'SELECT * FROM sh_goods ';

        if( !empty( $where ) ){

            $sql .= ' where ' . $where;

        }

        $query = self::$db->query( $sql );

        return $query->rows;
    }

    public static function search( $filter = [] ){

        $filter['name'] = !empty( $filter['name'] )? $filter['name'] : '';
        $sql = 'SELECT 
                  g.*
                FROM
                  sh_goods g 
                WHERE g.name like "%' . $filter['name'] .'%"'
                .' limit 0, 20';

        $query = self::$db->query( $sql );

        return $query->rows;
    }

    public static function info( $goods_id ){

        $sql = 'SELECT 
  g.*,
  gd.`description` 
FROM
  sh_goods g 
  LEFT JOIN sh_goods_description gd 
    ON g.`id` = gd.`goods_id` 
WHERE g.id = ' . $goods_id;
        $query = self::$db->query( $sql );

        return $query->row;
    }

    public static function edit( $goods_id, $data ){

        if( !empty( $goods_id ) ){
            $sql = "update sh_goods SET
                    `name`='" . $data['name'] . "',
                    price='" . $data['price'] . "',
                    image='" . $data['image'] . "',
                    `status`=" . $data['status'] ." where id=" . $goods_id ;
        }else{
            $sql = "INSERT INTO sh_goods SET
                    `name`='" . $data['name'] . "',
                    price='" . $data['price'] . "',
                    image='" . $data['image'] . "',
                    `status`=" . $data['status'] ;
        }
        $execute = self::$db->query( $sql );

        if( empty( $goods_id ) ){
            $goods_id = self::$db->getLastId();
        }

        if( !empty( $goods_id ) ){

            $data_description = [
                'description'=>$data['description']
            ];
            self::edit_goods_description($goods_id, $data_description);

            $data_images = $data['goods_images'];
            self::edit_goods_img( $goods_id, $data_images );

            $categories = $data['category'];
            self::edit_goods_category( $goods_id, $categories );

            $execute2 = self::$db->query( $sql );
        }

        return $execute->num_rows;

    }

    public static function get_goods_description( $goods_id ){

        $sql = "select * from sh_goods_description where goods_id=" . $goods_id;
        $query = self::$db->query( $sql );

        return $query->row;


    }

    public static function edit_goods_description( $goods_id, $data ){

        //todo 删除
        $delete_sql = "DELETE FROM sh_goods_description WHERE goods_id=" . $goods_id;
        $query = self::$db->query( $delete_sql );

        //todo 添加

        $sql = "INSERT INTO sh_goods_description SET 
                  goods_id='" . $goods_id . "',
                  description='"  . $data['description'] . "'" ;

        $query = self::$db->query( $sql );


    }

    public static function get_goods_images( $goods_id ){

        $sql = "select * from sh_goods_img where goods_id=" . $goods_id;
        $query = self::$db->query( $sql );

        return $query->rows;
    }

    public static function edit_goods_img( $goods_id, $data_arr ){

        //todo 删除
        $delete_sql = "DELETE FROM sh_goods_img WHERE goods_id=" . $goods_id;
        $query = self::$db->query( $delete_sql );

        $values = '';
        if( !empty($data_arr) ){
            foreach( $data_arr as $data ){
                $values .= "(" . $goods_id . ",'" . $data['url'] . "'," . $data['sort_order'] . "),";
            }
            $values = trim( $values,',' );

            //todo 添加
            $sql = "INSERT INTO sh_goods_img (goods_id, url, sort_order) VALUES " . $values ;

            $query = self::$db->query( $sql );
        }

    }

    public static function edit_goods_category( $goods_id, $categories ){

        $delete_sql = "delete from sh_goods_to_category where goods_id=" . $goods_id;
        $query = self::$db->query( $delete_sql );

        $values = '';
        if( !empty( $categories ) ){

            foreach( $categories as $category_id ){
                $values .= "(" . $goods_id . "," . $category_id . "),";
            }
            $values = trim( $values,',' );

            $insert_sql = "INSERT INTO sh_goods_to_category (goods_id, category_id) VALUES " . $values ;

            $query = self::$db->query( $insert_sql );

        }


    }

    public static function get_goods_category( $goods_id ){


        $sql = 'select * from sh_goods_to_category where goods_id=' . $goods_id;

        $query = self::$db->query( $sql );

        return $query->rows;

    }


}